
function sendNewTopic(title, message, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/ForumWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:createTopic xmlns:ns0="http://webservice/">' +
            '<title>' + title + '</title>' +
            '<message>' + message + '</message>' +
            '</ns0:createTopic>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}

function getTopicWithId(topic_id, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/ForumWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:getTopic xmlns:ns0="http://webservice/">' +
            '<topic_id>' + topic_id + '</topic_id>' +
            '</ns0:getTopic>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}

function sendNewPost(topic_id, message, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/ForumWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:post xmlns:ns0="http://webservice/">' +
            '<topic_id>' + topic_id + '</topic_id>' +
            '<message>' + message + '</message>' +
            '</ns0:post>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}

function getPostWithId(post_id, callback) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('POST', '/Trabalho_PD/ForumWebService', true);
    var request =
            '<?xml version="1.0" encoding="utf-8"?>' +
            '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
            '<soap:Body>' +
            '<ns0:getPost xmlns:ns0="http://webservice/">' +
            '<post_id>' + post_id + '</post_id>' +
            '</ns0:getPost>' +
            '</soap:Body>' +
            '</soap:Envelope>';
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState === 4) {
            if (xmlhttp.status === 200) {
                callback($.parseJSON(xmlhttp.responseXML.childNodes.item(0).textContent));
            }
        }
    };
    
    xmlhttp.onerror = function (e) {
        alert("Error contacting server!");
    };

    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send(request);
}